package net.sunzc.hwfacetest;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.hardware.Camera.CameraInfo;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.SurfaceView;
import android.view.Window;
import com.hanvon.face.HanvonfaceShowView;
import com.hanvon.face.IResultCallback;
import com.hanvon.faceRec.HWFaceLib;
import com.hanvon.faceRec.OUserInfo;
import net.sunzc.hwfacetest.utils.MyLog;
import net.sunzc.hwfacetest.utils.ReadFromFile;

@SuppressLint("NewApi")
public class FaceDelog extends Activity implements IResultCallback {

    private static final String FACE_DATA = "face_data";
    private static final String TAG = "FaceDelog";
    private static final String NAME = "name";
    /* 设置需要像素的宽*高--后边注释参数为次Camera支持的参数 */
    private static int PIXEL_WIDTH = 1280;// 1280,800,768,720,640,576,480,384,352
    private static int PIXEL_HEIGHT = 720;// 720,480,432,480,480,432,320,288,288
    private HanvonfaceShowView mShowSurfaceView;
    private boolean bShowAdd = false;
    private int speakstart = 0;
    private CountDownTimer mFaceDownTimer = null;

    public static String getRecognizeName(int resultCode, Intent intent) {
        if (resultCode == RESULT_OK && intent != null) {
            return intent.getStringExtra(NAME);
        }
        return "";
    }

    public static void faceRecognize(Activity activity, String faceDataPath, int faceRecognize) {
        Intent intent = new Intent(activity, FaceDelog.class);
        intent.putExtra(FACE_DATA, faceDataPath);
        activity.startActivityForResult(intent, faceRecognize);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//
        setContentView(R.layout.face_delog);
        Intent intent = this.getIntent();
        String facedata = intent.getStringExtra(FACE_DATA);
        faceDownTimer(40000);
        ReadFromFile.writeNew2Binary(facedata, HWFaceLib.getFaceFeatureFile(this));
        // 打开检测窗口
        mShowSurfaceView = (HanvonfaceShowView) findViewById(R.id.hanvonfaceShowView1);
        mShowSurfaceView.onInitSDK(640, 480);
        mShowSurfaceView.setSurfaceView((SurfaceView) findViewById(R.id.mSurfaceView02));
        mShowSurfaceView.onRegisterResultCallback(this);
    }

    private void faceDownTimer(long countmill) {
        mFaceDownTimer = new CountDownTimer(countmill, 10000) {

            @Override
            public void onTick(long l) {
                FaceDelog.this.onTick(l);
            }

            @Override
            public void onFinish() {
                FaceDelog.this.onFinish();
            }
        }.start();
    }

    protected void onTick(long l) {
    }

    protected void onFinish() {
        close();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        close();
    }

    private void close() {
        mFaceDownTimer.cancel();
        mFaceDownTimer = null;
        Intent intent = new Intent();
        setResult(RESULT_CANCELED, intent);
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mShowSurfaceView.onStartPreview(CameraInfo.CAMERA_FACING_FRONT);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mShowSurfaceView.onReleaseSDK();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mShowSurfaceView.onReleaseSDK();
    }

    @Override
    public void resultCallback(int nResultType, OUserInfo info, String strName, int nID, int nAgeGroup, int nAge, int nGender, int nExp, String strLog) {
        switch (nResultType) {
            case 0:
                Intent intent = new Intent();
                intent.putExtra(NAME, strName);
                setResult(RESULT_OK, intent);
                HWFaceLib.clearFaceFeatureFile(this);
                finish();
                break;
            case 4:// 打印日志
                MyLog.i(TAG, "正在验证……");
                break;
        }
    }
}
